home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / A / AE Sample (TC5) / AppleEventHandlers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-07  |  5.0 KB  |  155 lines  |  [TEXT/KAHL]

  1. /* File: AppleEventHandlers.c */
  2. #include    "AESimple.h"
  3.  
  4. static char    *OSTypeToPStr(OSType theType, char *s);
  5.  
  6. void InstallAppleEventHandlers()
  7. {
  8.     OSErr    err;
  9.     long    result;
  10.  
  11.     err = Gestalt(gestaltAppleEventsAttr, &result);
  12.     if (err == noErr) {    
  13.         (void)AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, (EventHandlerProcPtr)HandleOapp, 0, false);
  14.         (void)AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,   (EventHandlerProcPtr)HandleOdoc, 0, false);
  15.         (void)AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,  (EventHandlerProcPtr)HandlePdoc, 0, false);
  16.         (void)AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, (EventHandlerProcPtr)HandleQuit, 0, false);
  17.         /* The last handler we install is a "catch-all" for debugging */
  18.         (void)AEInstallEventHandler(typeWildCard,    typeWildCard,         (EventHandlerProcPtr)NotHandled, 0, false);
  19.     }
  20. } /* init_apple_events_hook */
  21.  
  22.  
  23. static char    *OSTypeToPStr(OSType theType, char *s)
  24. /* This is a useful debugging tool as it allows us to put an EventID or EventID */
  25. /* into a string so we can display it in a dialog */
  26. {
  27.     *s = 0x04;
  28.     BlockMove(&theType, &s[1], 4);    /* Use BlockMove to avoid odd address error on 68000 machines */
  29.     return s;
  30. }
  31.  
  32. pascal OSErr NotHandled (AEDescList *aevt, AEDescList *reply, long refCon)
  33. /* This routine is called for any unknown events that come in (it's installed into the */
  34. /* table with wild-card handlers) and it will put up a dialog that lets us know that a */
  35. /* "bad" Apple event has come in. */
  36. {
  37.     OSErr    err;
  38.     OSType    eventClass, eventID;
  39.     OSType    typeCode;
  40.     char    scratchClass[5], scratchID[5];
  41.     Size    actualSize;
  42.     short    itemHit;
  43.     
  44.     AEDesc    directParam;
  45.     
  46.  
  47.     err = MyInteractWithUser(TRUE);    /* Tell the user it's urgent */
  48.     if (err == noErr) {
  49.         AEGetAttributePtr(aevt, keyEventClassAttr, typeType, &typeCode, (Ptr)&eventClass, sizeof(eventClass), &actualSize);
  50.         AEGetAttributePtr(aevt, keyEventIDAttr, typeType, &typeCode, (Ptr)&eventID,    sizeof(eventID),    &actualSize);
  51.         ParamText((void *)OSTypeToPStr(eventClass, scratchClass), (void *)OSTypeToPStr(eventID, scratchID), (void *)"", (void *)"");
  52.         itemHit = Alert(1002, 0L);
  53.     }
  54.     return errAEEventNotHandled;
  55. } /* NotHandled */
  56.  
  57.  
  58. pascal OSErr HandleOapp (AEDescList *aevt, AEDescList *reply, long refCon)
  59. {
  60.     NewDisplayWindow();
  61.     return noErr;
  62. } /* NotHandled */
  63.  
  64.  
  65. pascal OSErr HandleOdoc (AEDescList *aevt, AEDescList *reply, long refCon)
  66. {
  67.     AEDesc        fileListDesc;
  68.     long        numFiles;
  69.     DescType    actualType;
  70.     long        actualSize;
  71.     AEKeyword    actualKeyword;
  72.     FSSpec        oneFile;
  73.     long        index;
  74.     OSErr        err;
  75.                         
  76.     /* The "odoc" and "pdoc" messages contain a list of aliases as the direct paramater.  */
  77.     /* This means that we'll need to extract the list, count the list's elements, and     */
  78.     /* then process each file in turn.                                                                                                         */
  79.     
  80.     /* Extract the list of aliases into fileListDesc */
  81.     err = AEGetKeyDesc( aevt, keyDirectObject, typeAEList, &fileListDesc );
  82.     if (err) return err;
  83.         
  84.     /* Count the list elements */
  85.     err = AECountItems( &fileListDesc, &numFiles);
  86.     if (err) return err;
  87.                 
  88.     /* now get each file from the list and process it. */
  89.     /* Even though the event contains a list of alises, the Apple Event Manager */
  90.     /* will convert each alias to an FSSpec if we ask it to. */
  91.     for (index = 1; index <= numFiles; index ++) {
  92.         err = AEGetNthPtr( &fileListDesc, index, typeFSS, &actualKeyword,
  93.                             &actualType, (Ptr)&oneFile, sizeof(oneFile), &actualSize);
  94.         if (err) {
  95.             AEDisposeDesc(&fileListDesc );
  96.             return err;
  97.         }
  98.         
  99.         NewDisplayWindow();
  100.         open_selected_file(&oneFile, FrontWindow());
  101.     }
  102.     return noErr;
  103. } /* HandleOdoc */
  104.  
  105.  
  106. pascal OSErr HandlePdoc (AEDescList *aevt, AEDescList *reply, long refCon)
  107. {
  108.     AEDesc        fileListDesc;
  109.     long        numFiles;
  110.     DescType    actualType;
  111.     long        actualSize;
  112.     AEKeyword    actualKeyword;
  113.     FSSpec        oneFile;
  114.     long        index;
  115.     OSErr        err;
  116.                         
  117.     /* The "odoc" and "pdoc" messages contain a list of aliases as the direct paramater.  */
  118.     /* This means that we'll need to extract the list, count the list's elements, and     */
  119.     /* then process each file in turn.                                                                                                         */
  120.     
  121.     /* Extract the list of aliases into fileListDesc */
  122.     err = AEGetKeyDesc( aevt, keyDirectObject, typeAEList, &fileListDesc );
  123.     if (err) return err;
  124.         
  125.     /* Count the list elements */
  126.     err = AECountItems( &fileListDesc, &numFiles);
  127.     if (err) return err;
  128.                 
  129.     /* now get each file from the list and process it. */
  130.     /* Even though the event contains a list of alises, the Apple Event Manager */
  131.     /* will convert each alias to an FSSpec if we ask it to. */
  132.     for (index = 1; index <= numFiles; index ++) {
  133.         err = AEGetNthPtr( &fileListDesc, index, typeFSS, &actualKeyword,
  134.                             &actualType, (Ptr)&oneFile, sizeof(oneFile), &actualSize);
  135.         if (err) {
  136.             AEDisposeDesc(&fileListDesc );
  137.             return err;
  138.         }
  139.         
  140.         NewDisplayWindow();
  141.         open_selected_file(&oneFile, FrontWindow());
  142.         if (ShowJobDialog(FrontWindow()))
  143.             PrintWindow(FrontWindow());
  144.         CloseAWindow(FrontWindow());
  145.     }
  146.     return noErr;
  147. } /* HandlePdoc */
  148.  
  149.  
  150. pascal OSErr HandleQuit (AEDescList *aevt, AEDescList *reply, long refCon)
  151. {
  152.     gDone = TRUE;
  153.     return noErr;
  154. } /* NotHandled */
  155.